$(function () {
  // 协同管理 打开
  var n;
  $(".cooCombatManage").on("click", function (e) {
    e.preventDefault();
    n = 1;
    cooManagePageList(n);
    $(".cooManage-pop").addClass("is-visible3");
  })
  // 协同管理 关闭
  $(".cooManage-pop").on("click", function (event) {
    if (
      $(event.target).is(".cd-popup-close1") ||
      $(event.target).is(".cooManage-pop") || $(event.target).is(".cd-popup-close")
    ) {
      event.preventDefault();
      $(this).removeClass("is-visible3");
      // 清空数据
      $("#cooManageModal").find("input").val('');
      $('#cooManageModal select').find("option:first").prop("selected", "selected");
    }
  });

  // 协同管理 分页
  var cooManagePageNum = 1;
  var cooManagePageNumCurr = 1;

  function cooManageListPageFunc(pageSum, startPageNum) {
    if (pageSum <= 1) {
      $('#cooManageModal .pageJump').hide()
    } else {
      $('#cooManageModal .pageJump').show()
    }
    Page({
      num: pageSum, //页码数
      startnum: startPageNum, //指定页码
      elem: $('#cooManagePage'), //指定的元素
      callback: function (n) { //回调函数
        console.log(n);
        contentArr = [];
        cooManagePageNumCurr = n;
        cooManagePageList(n);
      }
    });
  };

  // 定义一个详情需要的数组
  var contentArr = [];
  // 渲染 协同管理列表
  function cooManagePageList(n) {
    var orgId = localStorage.getItem('userOrgId');
    // 定义一个结束状态的字段
    var ifEndState = 0;
    $.ajax({
      // http://localhost:9092/workOrder/getHistory?initiator=%E8%B6%85%E7%BA%A7%E7%AE%A1%E7%90%86%E5%91%98&taskState=1&size=1&page=0
      url: '/workOrder/getHistory?size=6&page=' + (n - 1),
      type: 'GET',
      async: false,
      success: function (result) {
        console.log(result);
        var content = result.data.content;
        console.log(content);
        cooManagePageNum = result.data.totalPages;
        size = result.data.size;
        $('#cooManageModal .tbodyList').html('');
        for (var i = 0; i < content.length; i++) {
          contentArr.push(content[i]);
          // 任务状态   0 挂起，1驳回，2处理中，3忽略 4结束
          var taskStateText = '';
          if (content[i].taskState == 0) {
            taskStateText = '挂起';
            ifEndState = 0;
          } else if (content[i].taskState == 1) {
            taskStateText = '拒绝';
            ifEndState = 0;
          } else if (content[i].taskState == 2) {
            taskStateText = '处理中';
            ifEndState = 0;
          } else if (content[i].taskState == 3) {
            taskStateText = '忽略';
            ifEndState = 0;
          } else {
            taskStateText = '结束';
            ifEndState = 1;
          }
          var operationTime = content[i].operationTime;
          console.log(typeof operationTime);
          // operationTime = Number(operationTime);
          console.log(operationTime);
          operationTime = moment(operationTime).format("YYYY-MM-DD HH:mm:ss");
          console.log(operationTime);
          $('#cooManageModal .tbodyList').append('<tr><td class="lineHeight">' + ((n - 1) * size + i + 1) + '</td><td class="lineHeight">' + content[i].alarmAddress + '</td><td class="lineHeight">' + content[i].initiator + '</td><td class="lineHeight">' + operationTime + '</td><td class="lineHeight">' + taskStateText + '</td><td>' + content[i].alarmContact + '</td><td class="lineHeight"><span class="seeDetailsStyle seeDetailsCooMana" CooManageId="' + i + '" isEnd = "' + ifEndState + '" CooManageId1="' + content[i].id + '">查看详情</span><span class="end" CooManageId="' + content[i].id + '" taskS="' + content[i].taskState + '">结束</span></td></tr>');
        }
        cooManageListPageFunc(cooManagePageNum, n);
      }
    });
  }

  // 状态 改变时 做筛选   
  // $("#cooManageModal .taskState").on("change", function(event){
  //     var taskState = '';
  //     event.preventDefault();
  //     taskState = $("#cooManageModal .taskState").find("option:selected").val();
  // })
  // 搜索按钮 查询事件
  $("#cooManageSearchBtn").on("click", function () {
    // 获取输入内容
    var address = $("#cooManageModal").find(".cooManageSearchInput-pop").val();
    $.ajax({
      url: '/workOrder/getHistory?alarmAddress=' + address + '&size=6&page=' + (n - 1),
      type: 'GET',
      async: false,
      success: function (result) {
        console.log(result);
        var content = result.data.content;
        console.log(content);
        cooManagePageNum = result.data.totalPages;
        size = result.data.size;
        $('#cooManageModal .tbodyList').html('');
        for (var i = 0; i < content.length; i++) {
          var taskStateText = '';
          if (content[i].taskState == 0) {
            taskStateText = '挂起';
          } else if (content[i].taskState == 1) {
            taskStateText = '驳回';
          } else if (content[i].taskState == 2) {
            taskStateText = '处理中';
          } else if (content[i].taskState == 3) {
            taskStateText = '忽略';
          } else {
            taskStateText = '结束';
          }
          $('#cooManageModal .tbodyList').append('<tr><td>' + ((n - 1) * size + i + 1) + '</td><td>' + content[i].alarmAddress + '</td><td>' + content[i].alarmContact + '</td><td>' + content[i].operationTime + '</td><td>' + taskStateText + '</td><td>' + content[i].remarks + '</td><td><span class="seeDetailsStyle" CooManageId="' + i + '" CooManageId1="' + content[i].id + '">查看详情</span><span class="end" CooManageId="' + content[i].id + '" taskS="' + content[i].taskState + '">结束</span></td></tr>');
        }
        cooManageListPageFunc(cooManagePageNum, n);
      }
    });
  })

  // $(".orderTracking-pop").addClass("is-visible3");
  // 任务跟踪单 打开
  $("#cooManageModal").on("click", ".seeDetailsCooMana", function (event) {
    event.preventDefault();
    $(".orderTracking-pop").addClass("is-visible3");
    // 获取所有数据
    console.log(contentArr);
    // 获取详情对应的id
    var id = $(this).attr("CooManageId");
    var isEnd = $(this).attr("isEnd");
    console.log(isEnd);
    // var index = id - 1;
    var contentArrThis = contentArr[id];
    console.log(contentArrThis);
    // 赋值 报警时间 报警人 操作时间
    var alarmTime = contentArrThis.alarmTime;
    // alarmTime = Number(alarmTime);
    alarmTime = moment(alarmTime).format("YYYY-MM-DD HH:mm:ss");
    // console.log(alarmTime);
    $("#orderTrackingModal").find(".taskStepsBeginTimeText").text(alarmTime);
    $("#orderTrackingModal").find(".taskStepsCurrentTabInitiator").text(contentArrThis.initiator);
    var operationTime = contentArrThis.operationTime;
    // operationTime = Number(operationTime);
    operationTime = moment(operationTime).format("YYYY-MM-DD HH:mm:ss");
    // console.log(operationTime);
    $("#orderTrackingModal").find(".taskStepsCurrentTabOperationTime").text(operationTime);
    // 获取协同点数据
    $('#orderTrackingModal .cooPointAround .cooPointAroundTabBigBox').html('');
    // 记录各自状态的数量
    // var n1 = 0; // 应答
    // var n2 = 0; // 拒绝
    // var n3 = 0; // 无应答
    // for (var i = 0; i < 4; i++) {
    //   var itemStr = 'manualIdHistory' + (+i + 1);
    //   if (contentArrThis[itemStr]) {
    //     console.log(itemStr, contentArrThis[itemStr]);
    //     var cooPointAroundName = contentArrThis[itemStr].devTypeName;
    //     console.log(cooPointAroundName);
    //     var cooPointAroundAddress = contentArrThis[itemStr].address;
    //     console.log(cooPointAroundAddress);
    //     var accept = contentArrThis[itemStr].accept;
    //     console.log(accept);
    //     // 0 应答 ，1 拒绝 ， null无应答 
    //     var taskStateText = '';
    //     if (accept == 0) {
    //       n1++;
    //       taskStateText = '应答';
    //     } else if (accept == 1) {
    //       n2++;
    //       taskStateText = '拒绝';
    //     } else {
    //       n3++;
    //       taskStateText = '无应答';
    //     }
    //     $('#orderTrackingModal .cooPointAround .cooPointAroundTabBigBox').append('<div class="cooPointAroundTabBigList"><span style="width: 55.33%">' + cooPointAroundName + '/' + cooPointAroundAddress + '</span><span style="width: 24.33%;">' + taskStateText + '</span></div>');
    //   } else {
    //     break;
    //   }
    // }
    // $("#orderTrackingModal .taskStepsStates").find(".taskStepsStates1").text(n2);
    // $("#orderTrackingModal .taskStepsStates").find(".taskStepsStates2").text(n1);
    // $("#orderTrackingModal .taskStepsStates").find(".taskStepsStates3").text(n3);


    $.ajax({
      url: '/workOrder/getWorkOrder?workOrderId=' + $(this).attr('CooManageId1'),
      async: false,
      success: function (res) {
        console.log(res);
        var accept1 = 0;
        var accept2 = 0;
        var accept3 = 0;
        var accept;
        var itemStr;

        var taskState = res.data.taskState;
        if (taskState == 4) { //结束
          $('.taskStepsThird .curr').hide();
          $('.taskStepsForth .curr').show();
          $('.taskStepsThird .curr1').text('任务已确认');
        } else if (taskState == 2) {
          $('.taskStepsThird .curr').show();
          $('.taskStepsForth .curr').hide();
          $('.taskStepsThird .curr1').text('等待任务确认');
        }
        for (var i = 0; i < res.data.manualHistory.length; i++) {
          if (res.data.manualHistory[i]) {
            var taskStateText = '';
            accept = res.data.manualHistory[i].accept;
            var cooPointAroundName = res.data.manualHistory[i].devTypeName;
            var cooPointAroundAddress = res.data.manualHistory[i].address;
            if (accept == 0) {
              accept1++;
              taskStateText = '应答';
            } else if (accept == 1) {
              accept2++;
              taskStateText = '拒绝';
            } else {
              accept3++;
              taskStateText = '无应答';
            }
            $('#orderTrackingModal .cooPointAround .cooPointAroundTabBigBox').append('<div class="cooPointAroundTabBigList"><span style="width: 55.33%">' + cooPointAroundName + '/' + cooPointAroundAddress + '</span><span style="width: 24.33%;">' + taskStateText + '</span></div>');

          }
        }
        // 任务单对象形式上传数据
        // for (var i = 0; i < 10; i++) {
        //   itemStr = 'manualIdHistory' + (+i + 1);
        //   if(res.data[itemStr]){
        //     var taskStateText = '';
        //     accept = res.data[itemStr].accept;
        //     var cooPointAroundName = res.data[itemStr].devTypeName;
        //     var cooPointAroundAddress = res.data[itemStr].address;
        //     if (accept == 0) {
        //       accept1++;
        //       taskStateText = '应答';
        //     } else if (accept == 1) {
        //       accept2++;
        //       taskStateText = '拒绝';
        //     } else {
        //       accept3++;
        //       taskStateText = '无应答';
        //     }
        //     $('#orderTrackingModal .cooPointAround .cooPointAroundTabBigBox').append('<div class="cooPointAroundTabBigList"><span style="width: 55.33%">' + cooPointAroundName + '/' + cooPointAroundAddress + '</span><span style="width: 24.33%;">' + taskStateText + '</span></div>');

        //   }
        // }
        // if(res.data.manualIdHistory1){
        //   var taskStateText = '';
        //   accept = res.data.manualIdHistory1.accept;
        //   var cooPointAroundName = res.data.manualIdHistory1.devTypeName;
        //   var cooPointAroundAddress = res.data.manualIdHistory1.address;
        //   if (accept == 0) {
        //     accept1++;
        //     taskStateText = '应答';
        //   } else if (accept == 1) {
        //     accept2++;
        //     taskStateText = '拒绝';
        //   } else {
        //     accept3++;
        //     taskStateText = '无应答';
        //   }
        //   $('#orderTrackingModal .cooPointAround .cooPointAroundTabBigBox').append('<div class="cooPointAroundTabBigList"><span style="width: 55.33%">' + cooPointAroundName + '/' + cooPointAroundAddress + '</span><span style="width: 24.33%;">' + taskStateText + '</span></div>');
        // }

        // if(res.data.manualIdHistory2){
        //   accept = res.data.manualIdHistory2.accept;
        //   var cooPointAroundName = res.data.manualIdHistory2.devTypeName;
        //   var cooPointAroundAddress = res.data.manualIdHistory2.address;
        //   var taskStateText = '';
        //   if (accept == 0) {
        //     accept1++;
        //     taskStateText = '应答';
        //   } else if (accept == 1) {
        //     accept2++;
        //     taskStateText = '拒绝';
        //   } else {
        //     accept3++;
        //     taskStateText = '无应答';
        //   }
        //   $('#orderTrackingModal .cooPointAround .cooPointAroundTabBigBox').append('<div class="cooPointAroundTabBigList"><span style="width: 55.33%">' + cooPointAroundName + '/' + cooPointAroundAddress + '</span><span style="width: 24.33%;">' + taskStateText + '</span></div>');
        // }

        // if(res.data.manualIdHistory3){
        //   accept = res.data.manualIdHistory3.accept;
        //   var cooPointAroundName = res.data.manualIdHistory3.devTypeName;
        //   var cooPointAroundAddress = res.data.manualIdHistory3.address;
        //   var taskStateText = '';
        //   if (accept == 0) {
        //     accept1++;
        //     taskStateText = '应答';
        //   } else if (accept == 1) {
        //     accept2++;
        //     taskStateText = '拒绝';
        //   } else {
        //     accept3++;
        //     taskStateText = '无应答';
        //   }
        //   $('#orderTrackingModal .cooPointAround .cooPointAroundTabBigBox').append('<div class="cooPointAroundTabBigList"><span style="width: 55.33%">' + cooPointAroundName + '/' + cooPointAroundAddress + '</span><span style="width: 24.33%;">' + taskStateText + '</span></div>');
        // }

        // if(res.data.manualIdHistory4){
        //   accept = res.data.manualIdHistory4.accept;
        //   var cooPointAroundName = res.data.manualIdHistory4.devTypeName;
        //   var cooPointAroundAddress = res.data.manualIdHistory4.address;
        //   var taskStateText = '';
        //   if (accept == 0) {
        //     accept1++;
        //     taskStateText = '应答';
        //   } else if (accept == 1) {
        //     accept2++;
        //     taskStateText = '拒绝';
        //   } else {
        //     accept3++;
        //     taskStateText = '无应答';
        //   }
        //   $('#orderTrackingModal .cooPointAround .cooPointAroundTabBigBox').append('<div class="cooPointAroundTabBigList"><span style="width: 55.33%">' + cooPointAroundName + '/' + cooPointAroundAddress + '</span><span style="width: 24.33%;">' + taskStateText + '</span></div>');
        // }

        console.log(accept1, accept2, accept3)
        $("#orderTrackingModal .taskStepsStates").find(".taskStepsStates1").text(accept2);
        $("#orderTrackingModal .taskStepsStates").find(".taskStepsStates2").text(accept1);
        $("#orderTrackingModal .taskStepsStates").find(".taskStepsStates3").text(accept3);

            // 获取协同内容
        $("#orderTrackingModal").find(".cooContentTabBox").text(res.data.synergeticContent);
        // 获取备注
        $("#orderTrackingModal").find(".taskListRemark-pop").text(res.data.remarks);
        // 给结束按钮 添加自定义属性
        $("#orderTrackingModal .orderTrackingEndBtn-pop").attr("id", res.data.id);
        $("#orderTrackingModal .orderTrackingEndBtn-pop").attr("taskState", res.data.taskState);

      }
    });
    // // 获取协同内容
    // $("#orderTrackingModal").find(".cooContentTabBox").text(contentArrThis.synergeticContent);
    // // 获取备注
    // $("#orderTrackingModal").find(".taskListRemark-pop").text(contentArrThis.remarks);
    // // 给结束按钮 添加自定义属性
    // $("#orderTrackingModal .orderTrackingEndBtn-pop").attr("id", contentArrThis.id);
    // $("#orderTrackingModal .orderTrackingEndBtn-pop").attr("taskState", contentArrThis.taskState);

    // 加一个结束字样
    if (isEnd == 1) { // 表示结束状态
      $("#orderTrackingModal").find(".taskStepsForthEndText").show();
    } else {
      $("#orderTrackingModal").find(".taskStepsForthEndText").hide();
    }

  });
  // 任务跟踪单 关闭
  $(".orderTracking-pop").on("click", function (event) {
    if (
      $(event.target).is(".cd-popup-close1") ||
      $(event.target).is(".orderTracking-pop") || $(event.target).is(".cd-popup-close")
    ) {
      event.preventDefault();
      $(this).removeClass("is-visible3");
      //   contentArr = [];
      // 清除数据
      $("#orderTrackingModal .taskListRemark").find("textarea").val('');
      $("#orderTrackingModal .taskListData").find(".taskListDataAlarmAddress").val('');
      $("#orderTrackingModal").find(".taskStepsBeginTimeText").text('');
      $("#orderTrackingModal").find(".taskStepsCurrentTabInitiator").text('');
      $("#orderTrackingModal").find(".taskStepsCurrentTabOperationTime").text('');
      $('#orderTrackingModal .cooPointAround .cooPointAroundTabBigBox').html('');
      $("#orderTrackingModal").find(".cooContentTabBox").text('');
      // 清除结束 字样
      // $("#orderTrackingModal").find(".taskStepsForthEndText").hide();
      // ifEndState = false;
    }
  });
  // 任务跟踪单的取消按钮
  $("#orderTrackingModal .orderTrackingCancelBtn-pop").on("click", function (event) {
    $(".orderTracking-pop").removeClass("is-visible3");
    // 清除数据
    $("#orderTrackingModal .taskListRemark").find("textarea").val('');
    $("#orderTrackingModal .taskListData").find(".taskListDataAlarmAddress").val('');
    $("#orderTrackingModal").find(".taskStepsBeginTimeText").text('');
    $("#orderTrackingModal").find(".taskStepsCurrentTabInitiator").text('');
    $("#orderTrackingModal").find(".taskStepsCurrentTabOperationTime").text('');
    $('#orderTrackingModal .cooPointAround .cooPointAroundTabBigBox').html('');
    $("#orderTrackingModal").find(".cooContentTabBox").text('');
    // 清除结束 字样
    // $("#orderTrackingModal").find(".taskStepsForthEndText").hide();
    // ifEndState = false;
  });

  // 协同管理列表的  操作的 结束按钮
  $("#cooManageModal .tbodyList").on("click", ".end", function (e) {
    e.preventDefault();
    var id = $(this).attr("CooManageId");
    console.log(id);
    // var taskS = $(this).attr("taskS");
    $.ajax({
      // localhost:9092/workOrder/putTaskState?workOrderId=1&taskState=4
      url: '/workOrder/putTaskState?workOrderId=' + id + '&taskState=4',
      type: 'GET',
      success: function (result) {
        console.log(result);
        cooManagePageList(cooManagePageNumCurr);
      },
      error: function (res) {
        console.log(res);
      }
    });
  })
  // 任务跟踪单上的结束任务按钮
  $("#orderTrackingModal .orderTrackingEndBtn-pop").on("click", function (e) {
    e.preventDefault();
    var id = $(this).attr("id");
    // var taskS = $(this).attr("taskState");
    $.ajax({
      // localhost:9092/workOrder/putTaskState?workOrderId=1&taskState=4
      url: '/workOrder/putTaskState?workOrderId=' + id + '&taskState=4',
      type: 'GET',
      success: function (result) {
        console.log(result);
        $(".orderTracking-pop").removeClass("is-visible3");
        cooManagePageList(cooManagePageNumCurr);
      },
      error: function (res) {
        console.log(res);
      }
    });
  })
})